package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.dao.BlogCommentDao;
import com.qinge.blog.dao.BlogContentDao;
import com.qinge.blog.dao.BlogTypeDao;
import com.qinge.blog.dao.BlogUserDao;
import com.qinge.blog.entity.*;
import com.qinge.blog.service.BlogContentTagService;
import com.qinge.blog.service.BlogContentTypeService;
import com.qinge.blog.utils.BeanMapper;
import com.qinge.blog.utils.CurrentUserUtil;
import com.qinge.blog.vo.BlogContentVO;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.qinge.blog.service.BlogContentService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogContent service层实现类
 */
@Service
public class BlogContentServiceImpl implements BlogContentService {
    @Resource
    BlogContentDao contentDao;

    @Resource
    BlogUserDao userDao;

    @Resource
    BlogContentTypeService contentTypeService;

    @Resource
    BlogContentTagService contentTagService;

    @Resource
    BlogTypeDao typeDao;


    @Resource
    BlogCommentDao commentDao;

    @Override
    public void add(BlogContentVO... blogContentVOS) {
        for (BlogContentVO blogContentVO:blogContentVOS) {
            blogContentVO.setUserId(CurrentUserUtil.getUserId());
            blogContentVO.setTypeId(blogContentVO.getType().getId());
        }

        List<BlogContent> list=BeanMapper.mapList(Arrays.asList(blogContentVOS),BlogContent.class);


        contentDao.batchInsert(list);

        for (BlogContentVO each :blogContentVOS) {
            addOthers(each);
        };

    }

    @Override
    @Deprecated
    public void addByList(List<BlogContentVO> blogContentVOS) {
        List<BlogContent> list=BeanMapper.mapList(blogContentVOS,BlogContent.class);

        contentDao.batchInsert(list);
    }

    @Override
    public void removeById(String id) {
        BlogContentExample example=new BlogContentExample();
        BlogContentExample.Criteria criteria=example.createCriteria();

        criteria.andIdEqualTo(id);

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        contentDao.deleteByExample(example);

        contentTypeService.removeByConIds(id);

        contentTagService.removeByConIds(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogContentExample example=new BlogContentExample();
        BlogContentExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        contentDao.deleteByExample(example);

        removeOthers(ids);
    }

    @Override
    public void update(BlogContentVO blogContentVO) {
        contentDao.upsert(blogContentVO);

        removeOthers(Collections.singletonList(blogContentVO.getId()));

        addOthers(blogContentVO);
    }

    @Override
    public void upsert(BlogContentVO blogContentVO) {
        contentDao.upsert(blogContentVO);

        removeOthers(Collections.singletonList(blogContentVO.getId()));

        addOthers(blogContentVO);
    }

    @Override
    public BlogContentVO findById(String id) {
        BlogContentVO contentVO= new BlogContentVO();

        BeanMapper.copy(contentDao.selectByPrimaryKey(id),contentVO);

        contentVO.setUser(userDao.selectByPrimaryKey(contentVO.getUserId()));

        contentVO.setTags(contentTagService.findTagsByConId(id));

        contentVO.setType(contentTypeService.findTypeByConId(id));

        BlogCommentExample example=new BlogCommentExample();
        BlogCommentExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdEqualTo(id);

        contentVO.setComments(commentDao.selectByExample(example));

        return contentVO;
    }

    @Override
    public List<BlogContentVO> findByIds(List<String> ids) {
        BlogContentExample example=new BlogContentExample();
        BlogContentExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        List<BlogContentVO> contentVOS=contentDao.selectByExample(example).stream().map(each->{
            BlogContentVO contentVO=new BlogContentVO();

            contentVO.setTags(contentTagService.findTagsByConId(each.getId()));
            contentVO.setType(contentTypeService.findTypeByConId(each.getTypeId()));

            return contentVO;
        }).collect(Collectors.toList());

        return contentVOS;
    }

    @Override
    public PageInfo<BlogContentVO> findAll() {
        return paging(new PagingReqVO<BlogContentVO>());
    }

    @Override
    public PageInfo<BlogContentVO> findAll(PagingReqVO<BlogContentVO> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogContentVO> paging(PagingReqVO<BlogContentVO> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogContentExample example=new BlogContentExample();
        BlogContentExample.Criteria criteria=example.createCriteria();

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        if(pagingReqVO.getParams()!=null){
            BlogContentVO contentVO=pagingReqVO.getParams();
            if(!StringUtils.isBlank(contentVO.getTitle())){
                criteria.andTitleLike(contentVO.getTitle());
            }
            // if(!StringUtils.isBlank(contentVO.getTypeId())){
            //     criteria.andTypeIdEqualTo(contentVO.getTypeId());
            // }
        }


        List<BlogContentVO> contentVOS=contentDao.selectByExample(example).stream().map(each->{
            BlogContentVO contentVO=new BlogContentVO();

            BeanMapper.copy(each,contentVO);

            contentVO.setTags(contentTagService.findTagsByConId(each.getId()));
            contentVO.setType(contentTypeService.findTypeByConId(each.getId()));
            contentVO.setUser(userDao.selectByPrimaryKey(contentVO.getUserId()));
            return contentVO;
        }).collect(Collectors.toList());

        return new PageInfo<>(contentVOS);
    }

    private void addOthers(BlogContentVO contentVO){
        BlogContentType contentType=new BlogContentType();

        List<BlogTag> tagIds=contentVO.getTags();
        BlogType blogType=contentVO.getType();

        contentType.setTypeId(blogType.getId());

        contentType.setContentId(contentVO.getId());

        List<BlogContentTag> contentTags=tagIds.stream().map(each->{
            BlogContentTag contentTag=new BlogContentTag();
            contentTag.setTagId(each.getId());
            contentTag.setContentId(contentVO.getId());
            return contentTag;
        }).collect(Collectors.toList());

        contentTypeService.add(contentType);

        contentTagService.addByList(contentTags);
    }

    private void removeOthers(List<String> ids){
        contentTypeService.removeByConIdList(ids);

        contentTagService.removeByConIdList(ids);
    }
}

